import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {QueryGoodsUnitResponse, AddOrderItemKey, SubGoodsResponse} from "../apis/GoodsApi";
import {Page, Pageable, Slice} from "../utils/Page";
import {OffsetDateTime} from "@js-joda/core";


export function queryMarketOrderInMarket(id:number,pageable:Pageable){
    return fetcher.get<Slice<QueryMarketOrderInMarketResponse>>(`/markets/${id}/orders`,{
        ...pageable.toRequestParams()
    })
}

export interface QueryMarketOrderInMarketResponse{
    id:number
    createdBy:NumberIdAndName
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    status: MarketOrderStatus
    goodsKindAmount: number
}

export function createMarketOrder(marketId: number) {
    return fetcher.post<number>(`markets/${marketId}/orders`)
}

export function getMarketOrderDetail(id: number) {
    return fetcher.get<MarketOrderDetailResponse>(`markets/orders/${id}`, {
        includeItems: true
    })
}

export function getMarketOrderDetailWithoutItems(id: number) {
    return fetcher.get<MarketOrderDetailResponse>(`markets/orders/${id}`)
}

export interface MarketOrderDetailResponse {
    id: number
    createdDateTime: OffsetDateTime
    createdBy: NumberIdAndName
    market: NumberIdAndName
    items: Array<MarketOrderItem>
    submitDateTime?: OffsetDateTime
    deliveryDateTime?: OffsetDateTime
    number:string
}

export interface MarketOrderItem {
    id: number
    name:string
    barcode:string,
    amountInOrder:number
    inStore:number
    boxSpecification?:string
    unit?:NumberIdAndName
    units: QueryGoodsUnitResponse[]
    subGoods?:SubGoodsResponse
    note?:string
}

export interface AddMarketOrderItemRequest {
    amount: number
    goodsId: number
    unitId?:number
}

export function addMarketOrderItem(id: number, addMarketOrderItemRequest: AddMarketOrderItemRequest) {
    return fetcher.post(`markets/orders/${id}/items`, {}, addMarketOrderItemRequest)
}

export function deleteMarketOrderItem(orderId: number, goodsId: number) {
    return fetcher.delete(`markets/orders/${orderId}/items/${goodsId}`)
}

export function deleteMarketOrderItemSubGoods(orderId: number, goodsId: number,subGoodsId:number) {
    return fetcher.delete(`markets/orders/${orderId}/items/${goodsId}/sub/${subGoodsId}`)
}


export interface UpdateMarketOrderItemAmountRequest {
    amount: number
    unitId?: number
}

export function updateMarketOrderItemAmount(orderId: number, goodsId: number, updateMarketOrderItemAmountRequest: UpdateMarketOrderItemAmountRequest,) {
    return fetcher.patch(`markets/orders/${orderId}/items/${goodsId}/amount`, {}, updateMarketOrderItemAmountRequest)
}

export function updateMarketOrderItemSubAmount(orderId: number, goodsId: number, updateMarketOrderItemAmountRequest: UpdateMarketOrderItemAmountRequest,subGoodsId:number ) {
    return fetcher.patch(`markets/orders/${orderId}/items/${goodsId}/sub/${subGoodsId}/amount`, {}, updateMarketOrderItemAmountRequest)
}

export function updateMarketOrderItemNote(orderId: number, goodsId: number,note:string){
    return fetcher.patch(`markets/orders/${orderId}/items/${goodsId}/note`, {}, note)
}

export function updateMarketOrderItemSubNote(orderId: number, goodsId: number,note:string,subGoodsId:number){
    return fetcher.patch(`markets/orders/${orderId}/items/${goodsId}/sub/${subGoodsId}/note`, {}, note)
}

export interface QueryMarketOrderRequest {
    marketIds: number[]
    startDate?: string
    endDate?: string
    employeeIds: number[]
    status?:MarketOrderStatus
    number?:string
}

export function queryMarketOrders(queryMarketOrderRequest: QueryMarketOrderRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryMarketOrderResponse>>("markets/orders", {...queryMarketOrderRequest, ...pageable})
}

export interface QueryMarketOrderResponse {
    id: number
    createdBy: NumberIdAndName
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    goodsKindAmount: number
    market: NumberIdAndName
    status: MarketOrderStatus
    number:string
}

export type MarketOrderStatus = keyof typeof marketOrderStatusMap

export const marketOrderStatusMap =
    {
        "WAIT_SUBMIT": "待提交",
        "SUBMITTED": "已提交",
        "DELIVERY": "已出货",
    } as const
export const marketOrderStatuses = Object.keys(marketOrderStatusMap) as MarketOrderStatus[]

export function getAllMarketOrderCreatedEmployees() {
    return fetcher.get<Array<NumberIdAndName>>("markets/orders/employees", {projection: "summary"})
}

export function getMarketOrderCreatedEmployeeByMarketId(id: number) {
    return fetcher.get<Array<NumberIdAndName>>(`markets/${id}/orders/employees`, {projection: "summary"})
}

export function submitMarketOrder(id: number) {
    return fetcher.patch<number>(`markets/orders/${id}/submit`)
}

export function convertToDeliveryOrder(id: number) {
    return fetcher.post<number>(`markets/orders/${id}/delivery`)
}
export function deleteMarketOrder(id:number){
    return fetcher.delete<number>(`markets/orders/${id}`)
}

export function isExistsGoodsInMarketOrder(orderId:number,barcode:string){
    return fetcher.get<boolean>(`/markets/orders/${orderId}/items/barcodes/${barcode}/exists`)
}

export function addMarketOrderItemFromBarcode(orderId:number,barcode:string){
    return fetcher.post<MarketOrderItemInOrderSummary>(`/markets/orders/${orderId}/items/barcodes/${barcode}`)
}

export interface MarketOrderItemInOrderSummary{
    goodsId:number
    name:string
    barcode:string,
    amountInOrder:number
    inStore:number
    boxSpecification?:string
    unit?:NumberIdAndName
    units: QueryGoodsUnitResponse[]
    subGoods?:SubGoodsResponse
}

export function addMarketOrderItemsFromSubGoodsKeyList(orderId:number, orderItemKeys:AddOrderItemKey[]){
    return fetcher.post<MarketOrderItemInOrderSummary[]>(`/markets/orders/${orderId}/items/batchAdd`,{},orderItemKeys)
}

export function queryLatestUncommitedMarketOrder(){
    return fetcher.get<LatestUncommitedMarketOrder[]>("markets/orders/latestUncommited")
}

export interface LatestUncommitedMarketOrder{
    id:number
    number:string
    goodsAmount:number
    createdDateTime:OffsetDateTime
    createdBy:NumberIdAndName
    market:NumberIdAndName
}